package com.sunday.shoufen.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.ClearEditText;
import com.sunday.shoufen.R;
import com.sunday.shoufen.base.BaseApp;
import com.sunday.shoufen.entity.ConfirmBuy;
import com.sunday.shoufen.entity.Order;
import com.sunday.shoufen.entity.ShopDetail;
import com.sunday.shoufen.http.ApiClient;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static java.io.FileDescriptor.in;

/**
 * Created by 刘涛 on 2017/10/20.
 */

public class ScanBuyActivity extends BaseActivity {

    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.shop_addr)
    TextView shopAddr;
    @Bind(R.id.edit_pro_money)
    ClearEditText editProMoney;
    @Bind(R.id.edit_remark)
    ClearEditText editRemark;
    @Bind(R.id.txt_extra_title)
    TextView txtExtraTitle;
    @Bind(R.id.discount_info1)
    TextView discountInfo1;
    @Bind(R.id.discount_info2)
    TextView discountInfo2;

    private int type;
    private ShopDetail shopDetail;
    private long memberId;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan_buy);
        ButterKnife.bind(this);
        type = getIntent().getIntExtra("type", 2);
        shopDetail = (ShopDetail) getIntent().getSerializableExtra("shopDetail");
        memberId = BaseApp.getInstance().getMember().getId();
        initView();
    }

    private void initView() {
        if (shopDetail == null) {
            return;
        }
        titleView.setText(shopDetail.getShopName());
        shopAddr.setText(shopDetail.getAddress());
        if (shopDetail.getIsAddActive() == 1) {
            if (shopDetail.getCommissionScale() == 0) {
                discountInfo1.setVisibility(View.GONE);
            }
            discountInfo1.setText(new StringBuilder().append(String.format("赚消费积分%.2f", shopDetail.getCommissionScale())).append("%").toString());
            discountInfo2.setText(new StringBuilder().append(String.format("领活动通用券%.2f", shopDetail.getCouponCommission())).append("%").toString());
        } else {
            discountInfo2.setVisibility(View.GONE);
            discountInfo1.setText(new StringBuilder().append(String.format("赚消费积分%.2f", shopDetail.getCommissionScale())).append("%").toString());
        }
    }


    @OnClick(R.id.btn_confirm)
    void confirm() {
        String money = editProMoney.getText().toString().trim();
        String remark = editRemark.getText().toString().trim();
        if (TextUtils.isEmpty(money)) {
            ToastUtils.showToast(mContext, "请填写金额");
            return;
        }
        showLoadingDialog(0);
        Call<ResultDO<ConfirmBuy>> call = ApiClient.getApiAdapter().buyNow(shopDetail.getShopId(), memberId, null, null, money, remark);
        call.enqueue(new Callback<ResultDO<ConfirmBuy>>() {
            @Override
            public void onResponse(Call<ResultDO<ConfirmBuy>> call, Response<ResultDO<ConfirmBuy>> response) {
                if (isFinish || response.body() == null) {
                    return;
                }
                dissMissDialog();
                if (response.body().getCode() == 0) {
                    intent = new Intent(mContext, OrderPayActivity.class);
                    intent.putExtra("confirmBuy", response.body().getResult());
                    startActivity(intent);
                }
            }

            @Override
            public void onFailure(Call<ResultDO<ConfirmBuy>> call, Throwable t) {
                dissMissDialog();
                ToastUtils.showToast(mContext, R.string.network_error);
            }
        });

    }

}
